package com.ruoyi.web.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.account.service.IAccountItemSpecialService;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.db.domain.AccountItemSpecial;
import com.ruoyi.db.domain.SpecialCostomInfo;
import com.ruoyi.db.domain.SpecialCostomManagerInfo;
import com.ruoyi.db.mapper.SpecialCostomManagerInfoMapper;
import com.ruoyi.framework.shiro.service.SysPasswordService;
import com.ruoyi.special.service.ISpecialCostomInfoService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.service.ISpecialCostomInfoCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class SpecialCostomInfoCommonService implements ISpecialCostomInfoCommonService {


    @Autowired
    private ISpecialCostomInfoService specialCostomInfoService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private SysPasswordService passwordService;
    @Autowired
    private IAccountItemSpecialService accountItemSpecialService;

    @Override
    public Integer insertNewEntity(SpecialCostomInfo specialCostomInfo){

//        if (StrUtil.equals(specialCostomInfo.getInfoType(), "CRM")){
            specialCostomInfo = this.handleCrmCostomInfo(specialCostomInfo);
//        }

        if (ObjectUtil.isEmpty(specialCostomInfo)){
            return -1;
        }
        int i1 = specialCostomInfoService.insertSpecialCostomInfo(specialCostomInfo);

        if (i1 > 0){
            // 生成机构账户信息
            AccountItemSpecial accountItemSpecial = new AccountItemSpecial();
            accountItemSpecial.setInfoId(specialCostomInfo.getId());
            accountItemSpecialService.insertAccountItemSpecial(accountItemSpecial);
        }
        return i1;
    }

    /**
     * 处理crm 机构的信息
     * @param specialCostomInfo
     * @return
     */
    private SpecialCostomInfo handleCrmCostomInfo(SpecialCostomInfo specialCostomInfo){

        Long parentDeptId = 200L;
        SysDept lastDept = sysDeptService.selectLastDeptInfo();
        Long itemDeptId = lastDept.getDeptId() + 1;

        // 根据信息，生成部门数据
        SysDept sysDept = new SysDept();
        sysDept.setDeptId(itemDeptId);
        sysDept.setParentId(parentDeptId);
        sysDept.setDeptName(specialCostomInfo.getName());
        int i = sysDeptService.insertDept(sysDept);
        if (i <= 0){
            // 新增部门成功
            return null;
        }

        // 新增部门管理员 信息
        String loginName = RandomUtil.randomNumbers(9);
        String loginPwd = RandomUtil.randomString(6);

        Long[] roleIds = new Long[]{103L};

        SysUser sysUser = new SysUser();
        sysUser.setRoleIds(roleIds);
        sysUser.setDeptId(sysDept.getDeptId());
        sysUser.setLoginName(loginName);
        sysUser.setUserName(specialCostomInfo.getName());
        sysUser.setPassword(loginPwd);
        sysUser.setSalt(ShiroUtils.randomSalt());
        sysUser.setPassword(passwordService.encryptPassword(sysUser.getLoginName(), sysUser.getPassword(), sysUser.getSalt()));

        int i2 = sysUserService.insertUser(sysUser);
        if (i2 <= 0){
            return null;
        }

        // 新增机构信息
        specialCostomInfo.setDeptId(sysDept.getDeptId());
        specialCostomInfo.setAdminAccount(loginName);
        specialCostomInfo.setAdminPwd(loginPwd);

        return specialCostomInfo;
    }

}
